package ch12_容器框架与泛型.常用集合类;

import ch12_容器框架与泛型.Product;

public class HashedProduct extends Product {
	public HashedProduct(int id ,String name,int inventory) {
		super(id,name,inventory);
	}

	@Override
	public int hashCode() {

		return this.getName().hashCode();		//以名称为哈希码
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof HashedProduct) {
			return getName().equals(((HashedProduct) obj).getName()); //名称相同则相等
		}
		return false;			//若o不是HashedProduct对象则不相等
	}

	//将Product 对象转换为HashedProduct 对象
	public static HashedProduct from(Product p) {
		int id = p.getId();
		String name = p.getName();
		int inventory = p.getInventory();
		return new HashedProduct(id,name,inventory);
	}
}
